//
//  VVLookatMapController.m
//  XLife
//
//  Created by 恩佐John on 16/1/20.
//  Copyright © 2016年 恩佐John. All rights reserved.
//

#import "VVLookatMapController.h"
#import <BaiduMapAPI_Map/BMKMapComponent.h>
#import <BaiduMapAPI_Location/BMKLocationComponent.h>
#import <BaiduMapAPI_Utils/BMKUtilsComponent.h>
#import <BaiduMapAPI_Search/BMKSearchComponent.h>
#import "VVYLocationManager.h"
#import "LYPointAnnotation.h"
#import "LYTeacherWebViewController.h"


@interface VVLookatMapController ()<BMKMapViewDelegate,BMKLocationServiceDelegate>
@property (nonatomic, weak)BMKMapView *mapView;
@property (nonatomic, strong)BMKPointAnnotation *pointAnnotation;
@property (nonatomic, strong) BMKPointAnnotation *animatedAnnotation;
@property (nonatomic, strong)BMKLocationService *locationService;
@property (nonatomic, strong) UIButton *locationButton;
@end

@implementation VVLookatMapController

- (void)viewDidLoad {
    [super viewDidLoad];
    _locationService=[[BMKLocationService alloc]init];
    [self setUpMapCenter];
    [self setUpNac];
    [self MapAddPoint];
    
}
//设置导航栏
- (void)setUpNac
{
    self.navigationItem.title = @"查看地图";

}
////跳到第三方地图导航
//- (void)mapNaviga
//{
//    [[VVYLocationManager sharedManager] guideWithCoordinate:self.coordinate2D];
//
//}
// 设置地图的中心点
- (void)setUpMapCenter
{

    [self getUserCurrentLocation];

}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    [_mapView viewWillAppear];
    _mapView.delegate = self;
    _locationService.delegate = self;
}
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:YES];
    

}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:YES];
    [_mapView viewWillDisappear];
    _mapView.delegate=nil;
    _locationService.delegate=nil;
}
- (void)dealloc
{
    if (_mapView) {
        _mapView = nil;
        _locationService = nil;
    }
}

//添加大头针
-(void)MapAddPoint
{
    [_mapView removeOverlays:_mapView.overlays];
    [self addPointAnnotation];
    return;
}

//添加标注
- (void)addPointAnnotation
{
//    if (_pointAnnotation == nil) {
//        _pointAnnotation = [[BMKPointAnnotation alloc]init];
//        _pointAnnotation.coordinate = self.coordinate2D;
//        _pointAnnotation.title = self.hoteltitle;
////        _pointAnnotation.subtitle=self.hotelAddress;
//    }
    NSMutableArray *pointAnnotations = [NSMutableArray array];
    

    for (LYPersonalInfo *info in self.models) {
        
        CLLocationCoordinate2D coordinate = {info.lat.doubleValue, info.lng.doubleValue};
       LYPointAnnotation *pointAnnotation = [[LYPointAnnotation alloc]init];
        pointAnnotation.coordinate = coordinate;
        pointAnnotation.title = info.nick_name;
        pointAnnotation.info = info;
        [pointAnnotations addObject:pointAnnotation];
        
    }
    
    [_mapView addAnnotations:pointAnnotations];
    
}

- (BMKMapView *)mapView
{
    if (!_mapView) {
        BMKMapView* mapView = [[BMKMapView alloc]init];
        [mapView setZoomLevel:18];
        mapView.frame = self.view.frame;
        mapView.delegate = self;
        self.mapView = mapView;
        [self.view addSubview:mapView];
    }
    return _mapView;
}
#pragma mark  代理
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation
{
    
    //普通annotation
    NSString *AnnotationViewID = @"renameMark";
    BMKPinAnnotationView *annotationView = (BMKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
//    if (annotation == _pointAnnotation) {
        if (annotationView == nil) {
            annotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];
            // 设置颜色
            annotationView.pinColor = BMKPinAnnotationColorGreen;
            // 从天上掉下效果
            annotationView.animatesDrop = YES;
            //设置图片
            annotationView.image = [UIImage imageNamed:@"周边老师选中"];
            //设置信息
            annotationView.enabled = YES;
            
            annotationView.canShowCallout = NO;
        }
//    }
    return annotationView;

}

- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation{

    [_mapView updateLocationData:userLocation];
    //    [self.mapView setCenterCoordinate:userLocation.location];
    [self.mapView setCenterCoordinate:userLocation.location.coordinate animated:YES];
    [self.locationService stopUserLocationService];


}

- (void)mapView:(BMKMapView *)mapView didSelectAnnotationView:(BMKAnnotationView *)view
{
    
    [mapView deselectAnnotation:view.annotation animated:YES];
    LYPointAnnotation *pointAnnotation = (LYPointAnnotation *)view.annotation;
    LYPersonalInfo *info = pointAnnotation.info;
    LYTeacherWebViewController *teaVC = [LYTeacherWebViewController teaWebVCWithID:info.user_id];
    [self.navigationController pushViewController:teaVC animated:YES];
    
}
//在地图view将要启动定位时会调用此函数
- (void)willStartLocatingUser
{
    NSLog(@"即将开始定位");
}


/**
 *  获取用户当前位置信息
 */
- (void)getUserCurrentLocation
{

    [_locationService startUserLocationService];    
    //先关闭显示的定位图层
    self.mapView.showsUserLocation = NO;
    //设置定位状态
    _mapView.userTrackingMode = BMKUserTrackingModeFollow;
    
//    _mapView.showsUserLocation = YES;
    

    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
